const QRCode = require('qrcode')
const CurrentIPUtil = require('../util/CurrentIPUtil')
const ClickCopy = require('../util/ClickCopy')
const ClipboardJS = require('clipboard')
import './popup.css'

const clipboard = new ClipboardJS('.clipboard')
clipboard.on('success', function(e) {
    layer.msg('ip 地址复制成功');
    e.clearSelection();
});

// 显示当前标签页地址的二维码
chrome.tabs.query({ active: true, currentWindow: true }, ([activeTab]) => {
    document.getElementById("url").innerText = activeTab.url;
    QRCode.toCanvas(document.getElementById('canvas'), activeTab.url, { width: 128 }, (error) => {
        console.log(error);
    })
})

// 将用户输入显示成二维码
const $input = document.getElementById('text')
const $qrcodeName = document.getElementById('qrcodeName')
const $customQrcodeCanvas = document.getElementById('customQrcodeCanvas')
$input.addEventListener('blur', () => {
    let url = $input.value || "";
    $qrcodeName.innerText = url;
    QRCode.toCanvas($customQrcodeCanvas, url, { width: 128 }, (error) => {
        console.log(error);
    })
})

// 显示本机ipv4地址
CurrentIPUtil.getUserIP(ip=>{
    let el = document.getElementById("ip");
    el.innerText = ip;
    el.setAttribute("data-clipboard-text", el.innerHTML)
});